package com.egg.ylt.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.RecyclerViewUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.activity.ACT_OrderDetail;
import com.egg.ylt.adapter.ADA_OrderList;
import com.egg.ylt.pojo.AppointmentServeListEntity;
import com.egg.ylt.presenter.impl.OrderItemPresenterImpl;
import com.egg.ylt.view.IOrderItemView;
import com.yonyou.framework.library.base.BaseFragment;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.AppSharedPreferences;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.eventbus.EventCenter;

import java.util.List;

import butterknife.BindView;

/**
 * Created by sichaoqi on 2018/9/4.
 */

public class FRA_OrderItem extends BaseFragment<OrderItemPresenterImpl> implements IOrderItemView {

    @BindView(R.id.recycler_view_order)
    RecyclerView mOrderRlv;
    @BindView(R.id.swipe_refresh_order)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private String mStatus;
    private String pageSize = "20";
    private int listSize = 0;
    private int pageNum = 1;
    private String mUserPhone;

    private ADA_OrderList mOrderAdapter;
    private RecyclerViewUtil mRecyclerViewUtil;
    private int mRefreshStateInt = 0;

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {
        //请求订单数据
        if (mPresenter != null && !StringUtil.isEmpty(Constants.TOKEN)) {
            pageNum = 1;
            mPresenter.getOrderList(Constants.TOKEN, mUserPhone, pageSize, String.valueOf(pageNum), mStatus);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        //请求订单数据
        if (!hidden) {
            if (mPresenter != null && !StringUtil.isEmpty(Constants.TOKEN)) {
                pageNum = 1;
                mPresenter.getOrderList(Constants.TOKEN, mUserPhone, pageSize, String.valueOf(pageNum), mStatus);
            }
        }
    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected View getLoadingTargetView() {
        return mSwipeRefreshLayout;
    }

    @Override
    protected void initViewsAndEvents() {
        //初始化当前fragment
        int position = getArguments().getInt(FRA_OrderItem.class.getName(), -1);
        switch (position) {
            case 0:
                mStatus = position + "";
                break;
            case 1:
                mStatus = position + "";
                break;
            case 2:
                mStatus = position + "";
                break;
            case 3:
                mStatus = "";
                break;
        }

        AppSharedPreferences preferences = new AppSharedPreferences(mContext);
        mUserPhone = preferences.getString(Constants.USER_PHONE);

        //初始化recyclerView
        initView();

        //刷新和加载
        initRefresh();

        //请求订单数据
        if (mPresenter != null && !StringUtil.isEmpty(Constants.TOKEN)) {
            mPresenter.getOrderList(Constants.TOKEN, mUserPhone, pageSize, String.valueOf(pageNum), mStatus);
        }
    }

    private void initView() {
        //设置布局管理器
        mOrderRlv.setLayoutManager(new LinearLayoutManager(mContext));

        //设置适配器
        mOrderAdapter = new ADA_OrderList(mContext);
        mOrderRlv.setAdapter(mOrderAdapter);

        //item监听
        mOrderAdapter.setOnItemClickListener(new ADA_OrderList.OnItemClickListener() {
            @Override
            public void onClick(AppointmentServeListEntity.ListEntity entity) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("entity", entity);
                readyGo(ACT_OrderDetail.class, bundle);
            }
        });

    }

    //设置recyclerView的刷新和加载
    public void initRefresh() {
        //下拉刷新
        mSwipeRefreshLayout.setColorScheme(R.color.color_FE752D);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mRefreshStateInt = 0;
                pageNum = 1;
                mPresenter.getOrderList(Constants.TOKEN, mUserPhone, pageSize, String.valueOf(pageNum), mStatus);
            }
        });

        //设置上拉加载更多
        mRecyclerViewUtil = new RecyclerViewUtil(mOrderRlv);
        mRecyclerViewUtil.setRecyclerViewLoadMoreListener(new RecyclerViewUtil.RecyclerViewLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (listSize == Integer.parseInt(pageSize)) {
                    mRefreshStateInt = 1;
                    pageNum++;
                    //请求门店展示数据
                    mPresenter.getOrderList(Constants.TOKEN, mUserPhone, pageSize, String.valueOf(pageNum), mStatus);
                    mRecyclerViewUtil.setLoadMoreEnable(false);
                }
            }
        });
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fra_order_item;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    public static FRA_OrderItem newInstance(int status) {
        FRA_OrderItem fragment = new FRA_OrderItem();
        Bundle bdl = new Bundle(status);
        bdl.putInt(FRA_OrderItem.class.getName(), status);
        fragment.setArguments(bdl);
        return fragment;
    }

    @Override
    public void setOrderList(List<AppointmentServeListEntity.ListEntity> entities) {
        if (ListUtil.isListEmpty(entities)) {
            mSwipeRefreshLayout.setRefreshing(false);
            mRecyclerViewUtil.setLoadMoreEnable(true);
            toggleShowEmpty(true, R.drawable.empty_no_response, "空空如也~");
            return;
        }
        listSize = entities.size();
//        toggleShowEmpty(false, -1, "");

        //获取门店数据 0初始化和刷新状态   1 是滑到底部加载状态
        switch (mRefreshStateInt) {
            case 0:
                mOrderAdapter.addList(entities, listSize);
                //在onRefresh 里调用获取数据的方法就好了 数据获取完毕 别忘了 修改状态
                mSwipeRefreshLayout.setRefreshing(false);
                break;
            case 1:
                mOrderAdapter.updateList(entities, listSize);
                //加载完数据后回复上拉状态
                mRecyclerViewUtil.setLoadMoreEnable(true);
                break;
        }

    }
}
